package show;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.multimedia.jolitorask.security.AuthServlet;
import org.multimedia.jolitorask.telephone.TelephoneLocal;
import org.multimedia.jolitorask.utilisateur.UtilisateurLocal;

/**
 * Servlet implementation class for Servlet: ShowPhones
 *
 */
@SuppressWarnings("unchecked")
 public class ShowPhones extends AuthServlet implements javax.servlet.Servlet {
	 
   static final long serialVersionUID = 1L;
   public static int mode;
   public static int LOGIN = 1;
   public static int NAME_AND_FIRSTNAME = 2;
   public static int ALL = 0;
   
   private int size = 0;
   private String account, login, username, userfirstname, name, number;
   
   private JSONArray namePhones = new JSONArray();
   private JSONArray nameUsers = new JSONArray();
   
	public ShowPhones() {
		super();
		
		namePhones.put("name");
		namePhones.put("number");
		namePhones.put("priority");
		
		nameUsers.put("login");
		nameUsers.put("name");
		nameUsers.put("firstname");
	}   	
	
	
	private void syntax_error(PrintWriter out){
		
		JSONObject error = new JSONObject();
		
		try {
			error.put("account", account);
			error.put("size", 0);
			error.put("users", new JSONArray());
			out.println(error.toString());
		} catch (JSONException e) {
			out.println(""+null);
		}
		
	}
	
	
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

		mode = ALL;
		size = 0;
		
	    res.setContentType("text/plain");
	    PrintWriter out = res.getWriter();
	   
	    
	    /* On recupere le password */
	    if( (account=req.getParameter("account")) == null){
	    	syntax_error(out);
	    	return;
	    }
	    
	    
	    login = req.getParameter("login");
	    username = req.getParameter("username");
	    userfirstname =req.getParameter("userfirstname");
	    number = req.getParameter("number");
	    name = req.getParameter("name");
	    
	    if( login != null ){
	    	
	    	if (username != null || userfirstname != null) {
	    		syntax_error(out);
	    		return;
	    	}
	    	mode = LOGIN;

	    }
	    else {

	    	if (username != null || userfirstname != null) {
	    	
		    	if( username == null)
		    		username = "";
		    	if( userfirstname == null)
		    		userfirstname = "";
	    	
		    	mode = NAME_AND_FIRSTNAME;
	    	}
	    }
	    
	    if( number == null )
	    	number = "";
	    if( name == null )
	    	name = "";
	    
	    if( !authentificate(req)){
	    	out.println(false);
	    	return;
	    }

	    try {
	    	
	    	
	    	Iterator<UtilisateurLocal> iteUtilisateur = client.getUtilisateurs().iterator();
    		UtilisateurLocal u;
    		JSONObject json = new JSONObject();
    		JSONObject user = new JSONObject();
    		JSONArray array = new JSONArray();
    		
    		
	    	if(mode == ALL){

	    		while(iteUtilisateur.hasNext()){
	    			
		    		u = iteUtilisateur.next();
		    		user = new JSONObject();		
		    		user.put("login", u.getLogin());
		    		user.put("name", u.getNom());
		    		user.put("firstname", u.getPrenom());
		    		user.put("phones", getPhones(u, mode));
		    		
		    		array.put(user);
		    	}
	    	}
	    	else if( mode == LOGIN ){
	    		
	    		while(iteUtilisateur.hasNext()){

		    		u = iteUtilisateur.next();
		    		if( u.getLogin().equals(login) ){
		    		
		    			user = new JSONObject();
		    			user.put("login", u.getLogin());
			    		user.put("name", u.getNom());
			    		user.put("firstname", u.getPrenom());
			    		user.put("phones", getPhones(u, mode));
			    		
			    		array.put(user);
		    		}
		    	}
	    	}
	    	else if( mode == NAME_AND_FIRSTNAME ){
	    		
	    		while(iteUtilisateur.hasNext()){
	    			
	    			u = iteUtilisateur.next();
	    			if( u.getNom().contains(username) && u.getPrenom().contains(userfirstname)){
			    		
	    				user = new JSONObject();
			    		user.put("login", u.getLogin());
			    		user.put("name", u.getNom());
			    		user.put("firstname", u.getPrenom());
			    		user.put("phones", getPhones(u, mode));
			    		
			    		array.put(user);
	    			}
	    		}
	    	}
	    	
	    	json.put("account", account);
	     	json.put("size", size);
    		json.put("users", array);
    		out.println(json.toString());
    		
	    }
	    catch(Exception e){
	    	syntax_error(out);
	    	return;
	    }
	}  	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}
	
	
	
	private JSONArray getPhones(UtilisateurLocal u, int mode) throws JSONException{
	
		Iterator<TelephoneLocal> itelephone = u.getTelephones().iterator();
		TelephoneLocal t;
		JSONArray array;
		JSONArray phones = new JSONArray();
		while(itelephone.hasNext()){
			
			t = itelephone.next();
			if( (t.getNom() == null || t.getNom().contains(name) )
					&& (t.getNumero() == null || t.getNumero().contains(number))){
				
				array = new JSONArray();
				array.put(t.getNom());
				array.put(t.getNumero());
				array.put(t.getPriorite());
				phones.put(array.toJSONObject(namePhones));
				size++;
			}
		}
		
		return phones;
	}
	
	
}